/*
 * @(#)PaginationTag.java
 *
 * Copyright 2011 Xinhua Online, Inc. All rights reserved.
 */

package com.winxuan.channel.support.view.tags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.winxuan.util.pagination.DefaultPaginationConvertor;
import com.winxuan.util.pagination.LongParameterPaginationConvertor;
import com.winxuan.util.pagination.Pagination;
import com.winxuan.util.pagination.PaginationConvertor;

/**
 * 分页标签
 * @author  HideHai
 * @version 1.0,2011-6-21
 */
public class PaginationTag extends BodyTagSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6909818835438970544L;

	private Pagination pagination;
	private String bodyStyle;
	private boolean protect=false;
	private Integer proctectSize = 15;
	private PaginationConvertor convertor = null;


	//根据参数，在此方法判断是否忽略处理body
	//如果前台页数大于15页,则忽略body的输出
	@Override
	public int doStartTag() throws JspException {
		if(protect && pagination.getCurrentPage()>proctectSize){
			pagination.setCurrentPage(proctectSize);
		}
		if(bodyStyle!=null && bodyStyle.equals("javascript")){
			convertor = new LongParameterPaginationConvertor();
		}else{
			convertor = new DefaultPaginationConvertor();				
		}
		JspWriter writer = this.pageContext.getOut();
		try {
			writer.print(convertor.convert(pagination));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}


	@Override
	public void doInitBody() throws JspException {
		
	}

	@Override
	public void setBodyContent(BodyContent b) {
		super.setBodyContent(b);
	}

	@Override
	public int doEndTag() throws JspException {
		this.release();
		return EVAL_PAGE;
	}


	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}
	public void setBodyStyle(String bodyStyle) {
		this.bodyStyle = bodyStyle;
	}

	public void setProtect(boolean protect) {
		this.protect = protect;
	}

	public void setProctectSize(Integer proctectSize) {
		this.proctectSize = proctectSize;
	}

}

